package top.kaoshanji.p1ch09c;

import top.kaoshanji.p1ch09a.Note;

/**
 * 代码清单-p1ch09c-202页a：一个可扩展的程序
 * author kaoshanji
 * ^_^
 * create time 2025/3/9 18:47
 */
public class Music3 {

    // 并不关心类型，所以添加到系统的新类型仍然正常工作
    public static void tune(Instrument i) {
        i.play(Note.MIDDLE_C);
    }

    public static void tuneAll(Instrument[] e) {
        for (Instrument i: e) {
            tune(i);
        }
    }

    public static void main(String[] args) {
        // 在数组填充时会向上转型
        Instrument[] orchestra = {new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind()};

        tuneAll(orchestra);

    }

}

// 基类：乐器
class Instrument {
    void play(Note n) {
        System.out.println("Instrument.play() " + n);
    }
    String what() { return "Instrument"; }
    void adjust() {
        System.out.println("Adjusting Instrument");
    }
}

// 子类：一种乐器
class Wind extends Instrument {
    @Override void play(Note n) {
        System.out.println("Wind.play() " + n);
    }
    @Override String what() { return "Wind"; }
    @Override void adjust() {
        System.out.println("Adjusting Wind");
    }
}

// 子类：一种新乐器
class Percussion extends Instrument {
    @Override void play(Note n) {
        System.out.println("Percussion.play() " + n);
    }
    @Override String what() { return "Percussion"; }
    @Override void adjust() {
        System.out.println("Adjusting Percussion");
    }
}

// 子类：一种新乐器
class Stringed extends Instrument {
    @Override void play(Note n) {
        System.out.println("Stringed.play() " + n);
    }
    @Override String what() { return "Stringed"; }
    @Override void adjust() {
        System.out.println("Adjusting Stringed");
    }
}

// 子类：一种新乐器
class Brass extends Wind {
    @Override void play(Note n) {
        System.out.println("Brass.play() " + n);
    }
    @Override void adjust() {
        System.out.println("Adjusting Brass");
    }
}

// 子类：一种新乐器
class Woodwind extends Wind {
    @Override void play(Note n) {
        System.out.println("Woodwind.play() " + n);
    }
    @Override String what() { return "Woodwind"; }
}